package com.asion.vmovie.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.beans.SeriesBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asion on 16-10-18.
 */

public class XiLieRecyclerAdapter extends RecyclerView.Adapter<XiLieRecyclerAdapter.MyHolder> {

    private Context context;
    private List<SeriesBean.DataBean> list;

    public XiLieRecyclerAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    private OnItemClickListener onItemClickListener;
    private OnAddClickListener onAddClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnAddClickListener(OnAddClickListener onAddClickListener) {
        this.onAddClickListener = onAddClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.xilie_item, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        SeriesBean.DataBean dataBean = list.get(position);
        holder.xilieAdd.setOnClickListener(v->onAddClickListener.onAddClick(holder.xilieAdd,dataBean));
        Glide.with(context).load(dataBean.getImage()).asBitmap()
                .animate(R.anim.zoom_in)
                .centerCrop().error(R.mipmap.default_error).into(holder.xilieImage);
        holder.xilieTitle.setText(dataBean.getTitle());
        holder.xilieSubtitle.setText("已更新至"+dataBean.getUpdate_to()+"集    "+dataBean.getFollower_num()+"人已订阅");
        holder.xilieContent.setText(dataBean.getContent());
        holder.itemView.setOnClickListener(v -> onItemClickListener.onItemClick(dataBean));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.xilie_image)
        ImageView xilieImage;
        @BindView(R.id.xilie_title)
        TextView xilieTitle;
        @BindView(R.id.xilie_subtitle)
        TextView xilieSubtitle;
        @BindView(R.id.xilie_add)
        ImageView xilieAdd;
        @BindView(R.id.xilie_content)
        TextView xilieContent;
        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public void addAll(Collection collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }

    public void removeAll(){
        list.clear();
    }

    public interface OnItemClickListener{
        void onItemClick(SeriesBean.DataBean dataBean);
    }

    public interface OnAddClickListener{
        void onAddClick(View view,SeriesBean.DataBean dataBean);
    }
}
